
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Administrator on 2017/10/16.
 */

public class MyDataBaseHelper extends SQLiteOpenHelper {

    public static final String CREATE_BOOK = "create table Book ("
            + "id integer primary key autoincrement, "
            + "author text, "
            + "price real, "
            + "pages integer ,"
            + "name text)";

    public static final String CREATE_CATEGORY = "create table Category ("
            + "id integer primary key autoincrement,"
            + "category_name text, "
            + "category_code integer)";

    private Context mContext;

    public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.mContext = mContext;
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_BOOK);
        db.execSQL(CREATE_CATEGORY);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("drop table if exists Book");
        db.execSQL("drop table if exists Category");
        onCreate(db);
    }
}
//////////////////////////////a d s u
 SQLiteDatabase db;
        ContentValues values;
        switch (v.getId()){

            case R.id.add:

                 db = dbHelper.getWritableDatabase();
                values = new ContentValues();
                values.put("name","bob");
                values.put("author","bob");
                values.put("pages",33);
                values.put("price",22.22);
                db.insert("Book",null,values);
                values.clear();
                values.put("name","pop");
                values.put("author","bodb");
                values.put("pages",999);
                values.put("price",232.22);
                db.insert("Book",null,values);





                break;
            case R.id.delete:
                

                db = dbHelper.getWritableDatabase();
                db.delete("Book","pages > ?",new String[]{"1"});

                break;
            case R.id.select:
                db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("Book",null,null,null,null,null,null);
                if(cursor.moveToFirst()){

                    do{

                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        String page = cursor.getString(cursor.getColumnIndex("pages"));

                        Log.i("sql select",name+" "+author+" "+page+"\n");

                    }while(cursor.moveToNext());
                }


                break;
            case R.id.update:

                db = dbHelper.getWritableDatabase();
                values = new ContentValues();
                values.put("price","99");
                db.update("Book",values,"name=?",new String[]{"bob"});

                break;

            default:

                break;

        }

    }